//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vCamion.h"
#include "vSaisieType.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"


//------------------------------------------------------------------------------
bool __fastcall GereCamion( TOperationSaisie nOpe )
{
    bool bRetour = false;
    TvSCamion *fCamion = NULL;
    try {
        if (  (nOpe == OPE_Ajout)
            ||(nOpe == OPE_Modif)) {
            fCamion = new TvSCamion( Application, nOpe );
            bRetour = (fCamion->ShowModal() == mrOk);
        } else if (nOpe == OPE_Suppr) {
            if (MessageDlg("Etes-vous sūr(e) ?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes) {
                fCamion->Supprimer();
                //Supprimer();
                bRetour = true;
            }
        } else {
            assert( false );
        }

    } catch(...) {
        ShowMessage("Erreur !!!");
    }
    if (fCamion) { delete fCamion; fCamion = NULL; }
    return bRetour;
}
//---------------------------------------------------------------------------
__fastcall TvSCamion::TvSCamion(TComponent* Owner, TOperationSaisie nOpe )
    : TForm(Owner)
{
    m_nOpe = nOpe;

    if (m_nOpe == OPE_Modif) {
        NomEdit->Text                =     asMateriel(NOM);
        DescriptionEdit->Text        =     asMateriel(DESCRIPTION);
        PrixEdit->Text               =     fMateriel(PRIX);
        PoidsTotalEdit->Text         =     fCamion(POIDSTOTAL);
        PoidsTransportableEdit->Text =     fCamion(POIDSTRANSPORTABLE);
    }
    else {
      NomEdit->Clear();
      DescriptionEdit->Clear();
      PoidsTotalEdit->Clear();
      PoidsTransportableEdit->Clear();
      PrixEdit->Clear();
    }
                                                                                                                                                                                                                                                                  
}
//---------------------------------------------------------------------------

void __fastcall TvSCamion::bbOkClick(TObject *Sender)
{
    switch(m_nOpe)
    {
        case OPE_Ajout:
            Enregistrer();
            break;
        case OPE_Modif:
            Modifier();
            break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TvSCamion::Enregistrer()
{
    dmKiloutou->tMateriel->Insert();
    asMateriel(NOM)             = NomEdit->Text;
    asMateriel(DESCRIPTION)     = DescriptionEdit->Text;
    fMateriel(PRIX)             = PrixEdit->Text.ToDouble();
    nMateriel(TYPE_MATERIEL)    = TYPE_Camion;
    dmKiloutou->tMateriel->Post();

    dmKiloutou->trKiloutou->CommitRetaining();
    dmKiloutou->tMateriel->Refresh();
    dmKiloutou->tMateriel->Last();

    dmKiloutou->tCamion->Insert();
    fCamion(POIDSTOTAL)         = PoidsTotalEdit->Text.ToDouble();
    fCamion(POIDSTRANSPORTABLE) = PoidsTransportableEdit->Text.ToDouble();
    nCamion(CODE_MATERIEL)      = nMateriel(CODE_MATERIEL);
    dmKiloutou->tCamion->Post();

    dmKiloutou->trKiloutou->CommitRetaining();
}

//---------------------------------------------------------------------------

void __fastcall TvSCamion::Modifier()
{
    dmKiloutou->tMateriel->Edit();
    asMateriel(NOM)             = NomEdit->Text;
    asMateriel(DESCRIPTION)     = DescriptionEdit->Text;
    fMateriel(PRIX)             = PrixEdit->Text.ToDouble();
    dmKiloutou->tMateriel->Post();
    dmKiloutou->trKiloutou->CommitRetaining();

    dmKiloutou->tCamion->Edit();
    fCamion(POIDSTOTAL)         = PoidsTotalEdit->Text.ToDouble();
    fCamion(POIDSTRANSPORTABLE) = PoidsTransportableEdit->Text.ToDouble();
    dmKiloutou->tCamion->Post();
    dmKiloutou->trKiloutou->CommitRetaining();
}

//---------------------------------------------------------------------------

void __fastcall TvSCamion::Supprimer()
{
    dmKiloutou->tMateriel->Delete();
    dmKiloutou->trKiloutou->CommitRetaining();
    
    dmKiloutou->tCamion->Delete();
    dmKiloutou->trKiloutou->CommitRetaining();
}

//---------------------------------------------------------------------------

